package xyz.yeidi.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import xyz.yeidi.pojo.Business;
import xyz.yeidi.service.api.BusinessService;
import xyz.yeidi.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-02 19:46
 * @Version 1.0
 * @Describe :  得到所有的商家
 */
@WebServlet("/allBusiness")
public class AllBusinessServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BusinessService businessService = ServiceFactory.getBusinessService();
        List<Business> allBusiness = businessService.getAllBusiness();
        PrintWriter out = response.getWriter();
        ObjectMapper om = new ObjectMapper();
        if(allBusiness.isEmpty()){
            out.print("no_business");
        }else{
            out.print(om.writeValueAsString(allBusiness));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
